<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>用户登录</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 400px;
      margin: 100px auto;
      padding: 20px;
      background-color: #f5f5f5;
    }
    .login-container {
      background: white;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    h1 {
      text-align: center;
      color: #333;
      margin-bottom: 30px;
    }
    .form-group {
      margin-bottom: 20px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      color: #555;
    }
    input[type="text"], input[type="password"] {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
    }
    button {
      width: 100%;
      padding: 12px;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
    }
    button:hover {
      background-color: #0056b3;
    }
    .error {
      color: red;
      margin-top: 10px;
      text-align: center;
    }
    .demo-info {
      background-color: #e7f3ff;
      padding: 15px;
      border-radius: 4px;
      margin-bottom: 20px;
      border-left: 4px solid #007bff;
    }
  </style>
</head>
<body>
  <div class="login-container">
    <h1>双Token验证系统</h1>
    
    <div class="demo-info">
      <strong>演示账号：</strong><br>
      用户名: admin, 密码: password123<br>
      用户名: user1, 密码: 123456
    </div>
    
    <form id="loginForm">
      <div class="form-group">
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" required>
      </div>
      
      <div class="form-group">
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
      </div>
      
      <button type="submit">登录</button>
      
      <div id="errorMessage" class="error"></div>
    </form>
  </div>

  <script>
    document.getElementById('loginForm').addEventListener('submit', async function(e) {
      e.preventDefault();
      
      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const errorDiv = document.getElementById('errorMessage');
      
      try {
        const response = await fetch('/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({ username, password })
        });
        
        const data = await response.json();
        
        if (response.ok) {
          // 登录成功，保存token到localStorage
          localStorage.setItem('auth_token', data.authToken);
          alert('登录成功！');
          // 跳转到主页
          window.location.href = '/index.html';
        } else {
          errorDiv.textContent = data.error || '登录失败';
        }
      } catch (error) {
        errorDiv.textContent = '网络错误，请重试';
      }
    });
    
    // 如果已经登录，直接跳转到主页
    if (localStorage.getItem('auth_token')) {
      window.location.href = '/index.html';
    }
  </script>
</body>
</html>